package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.ResponseUtils;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {



    @Resource
    private IWorkerCertificationAuditService workerCertificationAuditService;
    @Resource
    private WorkerCertificationAuditMapper workerCertificationAuditMapper;

    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }


    /**
     * 新增实名认证信息
     * @param dto
     */
    @Override
    @Transactional
    public void addWorkerCertification(WorkerCertificationAuditAddReqDTO dto) {
        if(ObjectUtils.isEmpty(dto)){
            throw new CommonException("实名认证参数异常，不能为空");
        }
        //判断当前是否已经提交过申请
        try {
            WorkerCertification one = lambdaQuery()
                    .eq(WorkerCertification::getId, UserContext.currentUserId())
                    .eq(WorkerCertification::getCertificationStatus, CertificationStatusEnum.PROGRESSING.getStatus())
                    .one();
            if(!ObjectUtils.isEmpty(one)){
                throw new CommonException("当前用户已经有过实名认证正在审核中！");
            }
        } catch (Exception e) {
            throw new CommonException("异常！该账号已经存在两个及以上处于审核中的实名认证申请！");
        }
        WorkerCertification workerCertification = new WorkerCertification();
        BeanUtils.copyProperties(dto, workerCertification);
        workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        WorkerCertificationAudit audit = new WorkerCertificationAudit();
        BeanUtils.copyProperties(workerCertification, audit);
        workerCertification.setId(UserContext.currentUserId());
        save(workerCertification);
        //同时提交人员认证信息审批表
        audit.setServeProviderId(workerCertification.getId());
        workerCertificationAuditService.save(audit);
    }


    @Override
    public RejectReasonResDTO getRejectReason() {
        String rejectReason = workerCertificationAuditMapper.getRejectReason(UserContext.currentUserId());
        RejectReasonResDTO rejectReasonResDTO = new RejectReasonResDTO();
        rejectReasonResDTO.setRejectReason(rejectReason);
        return rejectReasonResDTO;
    }
}
